﻿using System.Linq;
using EasyIot.Common;
using EasyIot.Model;
using EasyIot.Repository;

namespace EasyIot.Service
{
    public class UserService : BaseService<UserModel>
    { 
        public static dynamic GetListByFilter(UserModel filter, PageInfo pageInfo)
        {
            pageInfo.prefix = "a.";
            string _where = " users a INNER JOIN role b ON a.RoleId=b.Id INNER JOIN organize c ON a.DepartmentId=c.Id";
            if (!string.IsNullOrEmpty(filter.Account))
            {
                _where += string.Format(" and {0}Account like concat('%',@Account,'%')", pageInfo.prefix);
            }
            if (!string.IsNullOrEmpty(filter.RealName))
            {
                _where += string.Format(" and {0}RealName like concat('%',@RealName,'%')", pageInfo.prefix);
            }
            if (filter.EnabledMark != null)
            {
                _where += string.Format(" and {0}EnabledMark=@EnabledMark", pageInfo.prefix);
            }
            _where = CreateTimeWhereStr(filter.StartEndDate, _where, pageInfo.prefix);
            pageInfo.returnFields = string.Format("{0}Id,{0}Account,{0}RealName,{0}Gender,c.FullName as 'DepartmentName',b.FullName as 'RoleName',{0}EnabledMark,{0}CreateTime,{0}SystemSign", pageInfo.prefix);
            return GetPageUnite(filter, pageInfo, _where);
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public static UserModel LoginOn(string username, string password)
        {
            return UserRepository.LoginOn(username, password);
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="model"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static int ModifyUserPwd(ModifyPwd model, int userId)
        {
            model.OldPassword = Md5.md5(model.OldPassword, 32);
            model.Password = Md5.md5(model.Password, 32);
            return UserRepository.ModifyUserPwd(model, userId);
        }

        public static bool IsExistCode(string account)
        {
            string where = "where Account=@Account";
            return GetByWhere(where, new { Account = account }).Count() > 0 ? true : false;
        }

        public static bool DeleteByIds(string[] idsArray)
        {
            return DeleteByWhere("where Id in @Ids and SystemSign=0", new { Ids = idsArray });
        }
    }
}
